import { _decorator, Component, Node, Label, AudioClip } from 'cc';
const { ccclass, property } = _decorator;

import CardFunctions from "../Common/CardFunctions";
import { Consts } from "../Data/Consts";
import { GameData } from "../Data/GameData";
import { bdlMgr } from "../Manager/SS_BundleManager";
import SS_GameManager from "../Manager/SS_GameManager";
import { SS_AudioManager } from '../Manager/SS_AudioManager';
import { SS_Settings } from '../Data/SS_Settings';

@ccclass('FailPanel')
export default class FailPanel extends Component {
    public static Instance: FailPanel;
    @property(Node)
    public Button_reborn: Node | null = null;
    @property(Node)
    public Button_restart: Node | null = null;
    @property(Node)
    public 复活: Node | null = null;
    @property(Label)
    public Label_reborn_time: Label | null = null;

    onLoad() {
        FailPanel.Instance = this;
    }

    async ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();

        bdlMgr.getRes<AudioClip>(Consts.BundleName.AUDIO, `sound/再努力一下`, AudioClip).then(async (audio) => {
            SS_AudioManager.Instance.playOneShot(audio, SS_Settings.curMusicV);
        });
    }

    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }

    Refresh() {
        this.复活.active = GameData.本关剩余复活次数 > 0;
        this.Label_reborn_time.string = `本关复活次数：${GameData.本关剩余复活次数}（刷新关卡重置）`
    }

    AddListener() {
        this.Button_reborn.on(Node.EventType.TOUCH_START, this.On_Button_reborn, this);
        this.Button_restart.on(Node.EventType.TOUCH_START, this.On_Button_restart, this);
    }
    RemoveListener() {
        this.Button_reborn.off(Node.EventType.TOUCH_START, this.On_Button_reborn, this);
        this.Button_restart.off(Node.EventType.TOUCH_START, this.On_Button_restart, this);
    }

    On_Button_reborn() {
        CardFunctions.AD_callback(() => {
            this.ShowOff();
            SS_GameManager.Instance.复活();
        }, Consts.埋点事件.视频广告_复活);
    }

    On_Button_restart() {
        this.ShowOff();
        SS_GameManager.Instance.ReStartGame();
    }
}

/** 调试 */
window["FailPanel"] = FailPanel;